package com.www.applyword.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.allen.library.SuperTextView;
import com.classic.adapter.CommonRecyclerAdapter;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.adapter.HomeJobAdapter2;
import com.www.applyword.base.BaseActivity;
import com.www.applyword.bean.HomeJobDetailBean;
import com.www.applyword.bean.JobListBean;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.utils.ImageUtil;
import com.www.applyword.utils.ListDataSave2;
import com.www.applyword.utils.Utils;


import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author : huang
 * date   : 2020/7/30 0030 9:55
 * version: 1.0
 */
public class HomeJobActivity extends BaseActivity {
    @BindView(R.id.tv_Order_Title)
    TextView tvOrderTitle;
    @BindView(R.id.tv_Order_Salary)
    TextView tvOrderSalary;
    @BindView(R.id.tv_Order_Num)
    TextView tvOrderNum;
    @BindView(R.id.tv_jiesai_type)
    TextView tvJiesaiType;
    @BindView(R.id.tv_area)
    TextView tvArea;
    @BindView(R.id.tv_Order_CreateTime)
    TextView tvOrderCreateTime;
    @BindView(R.id.tv)
    TextView tv;
    @BindView(R.id.tv_Order_Sex)
    TextView tvOrderSex;
    @BindView(R.id.rl_job_workfare)
    RecyclerView rlJobWorkfare;
    @BindView(R.id.tv_Order_PositionDescribe)
    TextView tvOrderPositionDescribe;
    @BindView(R.id.tv_counsel)
    TextView tv_counsel;
    @BindView(R.id.ll_counsel)
    LinearLayout llCounsel;
    @BindView(R.id.ll_apply)
    LinearLayout llApply;
    @BindView(R.id.st_apply)
    SuperTextView st_apply;
    @BindView(R.id.st_company)
    SuperTextView stCompany;
    @BindView(R.id.rl_job2)
    RecyclerView rlJob2;

    private int jobId;
    private  JobListBean jobListBeans;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.home_job_activity);
        setTitle("岗位详情");
    }

    @Override
    protected void loadData() {
        rlJob2.setLayoutManager(new LinearLayoutManager(this));
        rlJob2.setItemAnimator(new DefaultItemAnimator());
        HomeJobAdapter2 homeJobAdapter = new HomeJobAdapter2(this);
        rlJob2.setAdapter(homeJobAdapter);

        int id = getIntent().getIntExtra("id", 0);
        jobListBeans = (JobListBean) getIntent().getSerializableExtra("jobListBeans");
        Map<String, Object> params2 = new HashMap<>();
        params2.put("partJobId", id + "");
        params2.put("appKey", "QTSHE_ANDROID_MJ_WATERMELON");
        params2.put("townId", "87");
        params2.put("version", "4.35.0");
        //小时
        params2.put("versionCode", "10106");
        //职位福利
        params2.put("channel", "4");
        params2.put("downloadSource", "4");
        params2.put("deviceId", "869468021274583~ffe56626c3a6a4a9");
        params2.put("timestamp", "1596014357935");
        params2.put("sign", "8ef914390a28d46c7b65c8b20d22b99c");

        HttpClient.post(this, Constants.DETAIL, params2, new CallBack<HomeJobDetailBean>() {
            @Override
            public void onSuccess(HomeJobDetailBean homeJobDetailBean) {
                 jobId =  homeJobDetailBean.getPartJobId();
                tvOrderTitle.setText(homeJobDetailBean.getTitle());
                tvOrderSalary.setText(homeJobDetailBean.getSalary());
                tvOrderNum.setText("招聘人数：" + homeJobDetailBean.getJobCount());
                tvArea.setText(homeJobDetailBean.getCycleType().getValue());
                tvOrderSex.setText(homeJobDetailBean.getJobDesc());
                //  tv.setText(homeJobDetailBean.getJobDesc());

                ImageUtil.setImage(stCompany.getLeftIconIV(), homeJobDetailBean.getCompany().getLogo());
                stCompany.getLeftTopTextView().setText(homeJobDetailBean.getCompany().getName());
                stCompany.getLeftBottomTextView().setText(homeJobDetailBean.getCompany().getName());

                homeJobAdapter.replaceAll(homeJobDetailBean.getRecommendList(), true);
                List<JobListBean> strings = ListDataSave2.getInstence(HomeJobActivity.this).getDataList2();
                if (null != strings && strings.size() > 0){
                    for (JobListBean string : strings) {
                        if (string.getPartJobId() == jobId){
                            tv_counsel.setText("已收藏");
                        }
                    }
                }

                List<JobListBean> strings2 = ListDataSave2.getInstence(HomeJobActivity.this).getDataList();
                if (null != strings2 && strings2.size() > 0){
                    for (JobListBean string : strings2) {
                        if (string.getPartJobId() == jobId){
                            st_apply.getCenterTopTextView().setText("已报名");
                        }
                    }
                }
            }
        });

        homeJobAdapter.setOnItemClickListener(new CommonRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int i) {
                startActivity(new Intent(HomeJobActivity.this, HomeJobActivity.class).putExtra("id", homeJobAdapter.getItem(i).getPartJobId()));
            }
        });
    }



    @OnClick({R.id.ll_counsel, R.id.st_apply})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_counsel:
                counsel();
                break;
            case R.id.st_apply:
                jobApply();
                break;
                default:
        }
    }

    private void counsel() {
        List<JobListBean> strings = ListDataSave2.getInstence(this).getDataList2();
        if (null != strings && strings.size() > 0){
            for (JobListBean string : strings) {
                if (string.getPartJobId() == jobId){
                    Utils.showToastSafe("已收藏");
                    return;
                }
            }
        }

        ListDataSave2.getInstence(this).addDataList2(jobListBeans);
        Utils.showToastSafe("收藏成功");
        tv_counsel.setText("已收藏");
    }

    private void jobApply() {
        Map<String, Object> params2 = new HashMap<>();
        params2.put("eventId", "103010011001");
        params2.put("OTHER", "103010011001");
        params2.put("modifyUserInfo", "false");
        params2.put("partJobId", jobId + "");
        params2.put("imei","869468021274583");
        params2.put("androidid","ffe56626c3a6a4a9");


        params2.put("appKey", "QTSHE_ANDROID_MJ_WATERMELON");
        params2.put("townId", "87");
        params2.put("version", "4.35.0");
        //小时
        params2.put("versionCode", "10106");
        //职位福利
        params2.put("channel", "4");
        params2.put("downloadSource", "4");
        params2.put("deviceId", "869468021274583~ffe56626c3a6a4a9");
        params2.put("timestamp", "1596014357935");
        params2.put("sign", "8ef914390a28d46c7b65c8b20d22b99c");
        List<JobListBean> strings = ListDataSave2.getInstence(this).getDataList();
        if (null != strings && strings.size() > 0){
            for (JobListBean string : strings) {
                if (string.getPartJobId() == jobId){
                    Utils.showToastSafe("已报名");
                    return;
                }
            }
        }



        ListDataSave2.getInstence(this).addDataList(jobListBeans);
        Utils.showToastSafe("报告成功");

       /* HttpClient.post(this, Constants.JOBAPPLY, params2, new CallBack<ResultBean>() {
            @Override
            public void onSuccess(ResultBean homeJobDetailBean) {

            }
        });*/
    }
}
